package com.example.new6.views;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.example.new6.Test1Activity;


import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.new6.R;
import com.example.new6.adapter.HotAdapter;
import com.example.new6.entity.Hot;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HotActivity extends AppCompatActivity {
    private ListView hotSearchListView;

    private Spinner hotType;
    private HotAdapter adapter;
    private List<Hot> mHot = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot);

        hotSearchListView = findViewById(R.id.hot_list);
        hotType = findViewById(R.id.hot_type);
        adapter = new HotAdapter(this, mHot);
        hotSearchListView.setAdapter(adapter);


// 设置Spinner
        String[] types = new String[]{"哔哩哔哩", "百度", "知乎", "微信", "抖音", "腾讯新闻", "历史上的今天", "今日头条", "微博"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, types);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        hotType.setAdapter(adapter);
        hotType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedType = (String) parent.getItemAtPosition(position);
                fetchHotSearchData(getPlatformType(selectedType));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        fetchHotSearchData("bilibili");
    }

    // 根据用户友好的名称获取实际的平台类型
    private String getPlatformType(String friendlyName) {
        switch (friendlyName) {
            case "百度":
                return "baidu";
            case "知乎":
                return "zhihu";
            case "微信":
                return "weixin";
            case "哔哩哔哩":
                return "bilibili";
            case "抖音":
                return "douyin";
            case "腾讯新闻":
                return "news";
            case "历史上的今天":
                return "lishi";
            case "今日头条":
                return "toutiao";
            case "微博":
                return "weibo";
            default:
                return "bilibili"; // 默认值
        }
    }

    private void fetchHotSearchData( String type) {
        OkHttpClient client = new OkHttpClient();
        String url = "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=" + type;
        //type:平台:[baidu]百度；[zhihu]知乎；[weixin]微信；[bilibili]哔哩哔哩；[douyin]抖音；
        // [news]腾讯新闻；[lishi]历史上的今天；[toutiao]今日头条；[weibo]微博
        Request request = new Request.Builder()
                .url(url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                runOnUiThread(() -> Toast.makeText(HotActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    final String jsonData = response.body().string();
                    runOnUiThread(() -> {
                        try {
                            JSONObject jsonObject = new JSONObject(jsonData);
                            if (jsonObject.getInt("code") == 200) {
                                JSONArray results = jsonObject.getJSONArray("result");
                                mHot.clear();
                                for (int i = 0; i < results.length(); i++) {
                                    JSONObject item = results.getJSONObject(i);
                                    Hot hot = new Hot(
                                            item.getString("title"),
                                            item.getInt("index"),
                                            item.getString("hotValue"),
                                            item.getString("link")
                                    );
                                    mHot.add(hot);
                                }
                                adapter.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(HotActivity.this, "解析数据失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }


}
